package work.month05.day21;
/**
 * @Date 2020/05/21
 * @author 王光浩
 * @Need 定义一个 Triangle  类，继承Shape 类
 * 		构造方法：
 * 			为 Shape 的 type 字段赋值。
 * 		字段：
 * 			（1）protected double firstEdge ; 第一条边
 * 			（2）protected double secondEdge ;第二条边
 * 			（3）protected double secondEdge ;第三条边
 * 		方法：
 * 			（1）public void calculate()  计算三角形面积，赋值给 area 字段
 * 			（2）public void description() 输出三条边信息，然后调用父类show方法
 * 			（3）public void setFirstEdge(double firstEdge) 设置 firstEdge 值
 * 			（4）public void setSecondEdge(double secondEdge) 设置 secondEdge 值
 * 			（5）public void setThirdEdge(double thirdEdge) 设置 thirdEdge 值
 */
public class Triangle extends Shape{
	
	protected double firstEdge;  //第一条边
	protected double secondEdge; //第二条边
	protected double thirdEdge; //第三条边
	
	/**
	 * 构造一个 Triangle 对象，并为 type 赋值。
	 */
	public Triangle() {
		this.type = "三角形";
	}
	
	/**
	 * 设置第一条边长值
	 * @param secondEdge 边长值
	 */
	public void setFirstEdge(double firstEdge) {
		this.firstEdge = this.set(firstEdge);
	}
	
	/**
	 * 设置第二条边长值
	 * @param secondEdge 边长值
	 */
	public void setSecondEdge(double secondEdge) {
		this.secondEdge = this.set( secondEdge );
	}
	
	/**
	 * 设置第三条边长值
	 * @param secondEdge 边长值
	 */
	public void setThirdEdge(double thirdEdge) {
		this.thirdEdge = this.set( thirdEdge ); 
	}
	
	/**
	 * 如果三边能够组成三角形，则将三角形的面积赋给 area；否则报错
	 */
	public void calculate() {
		if ((firstEdge + secondEdge) <= thirdEdge 
				|| (firstEdge + thirdEdge) <= secondEdge
				|| (secondEdge + thirdEdge) <= firstEdge)
			throw new RuntimeException("三边不能组成三角形");
		double p = (firstEdge + secondEdge + thirdEdge) / 2;
		this.area = Math.sqrt(p * (p - firstEdge) * (p - secondEdge) * (p - thirdEdge));
	}
		
	/**
	 * 判断边长是否能够为负值
	 * @param edge	边长	
	 * @return	如果 edge 为负值，return 0.0 ；否则 return edge
	 */
	private double set(double edge) {
		if (edge > 0 )
			return edge;
		System.out.println( "边长不能为负值或0" );
		return 0.0;
	}
	
	/**
	 * 输出三角形的相关信息
	 */
	public void description() {
		System.out.printf("三角形的三边长为：%.2f,%.2f,%.2f", this.firstEdge, this.secondEdge, this.thirdEdge);
		System.out.println();
		this.show();
		System.out.println();
	}
}
